<?php


namespace app\partner\controller;


use think\Db;

class Orders extends Common
{
    /**
     * 淘宝订单
     */
    public function tbOrders()
    {
        $id = $this->id;
//        $id = 1;
        $input = input();
        $where = [];
        $where['u.super_partner_id'] = $id;
        $order_par_id = isset($input['order_no']) ? $input['order_no'] : '';
        if (!empty($order_par_id)) {
            $where['o.order_par_id'] = $order_par_id;
        }
        $order_time_start = isset($input['order_time_start']) ? strtotime($input['order_time_start']) : '';
        $order_time_end = isset($input['order_time_end']) ? strtotime($input['order_time_end']) : '';
        if (!empty($order_time_start) && !empty($order_time_end)) {
            $where['order_time'] = ['between', [$order_time_start, $order_time_end]];
        }
        $order_type = isset($input['order_type']) ? $input['order_type'] : '';
        if (!empty($order_type)) {
            $where['order_type'] = $order_type;
        }
        $order_status = isset($input['order_status']) ? $input['order_status'] : '';
        if (!empty($order_status)) {
            if ($order_status == -1) {
                $order_status = 0;
            }
            $where['order_status'] = $order_status;
        }
        $order_ok_time_start = isset($input['order_ok_time_start']) ? strtotime($input['order_ok_time_start']) : '';
        $order_ok_time_end = isset($input['order_ok_time_end']) ? strtotime($input['order_ok_time_end']) : '';
        if (!empty($order_ok_time_start) && !empty($order_ok_time_end)) {
            $where['order_ok_time'] = ['between', [$order_ok_time_start, $order_ok_time_end]];
        }
        $nickname = isset($input['nickname']) ? $input['nickname'] : '';
        if (!empty($nickname)) {
            $where['u.nickname'] = $nickname;
        }
        $par_name = isset($input['par_name']) ? $input['par_name'] : '';
        if (!empty($par_name)) {
            $where['p.nickname'] = $par_name;
        }
        $is_rebate = isset($input['is_rebate']) ? $input['is_rebate'] : '';
        if (!empty($is_rebate)) {
            if ($is_rebate == -1) {
                $is_rebate = 0;
            }
            $where['is_rebate'] = $is_rebate;
        }
        //返利时间
        $insert_time_start = isset($input['insert_time_start']) ? $input['insert_time_start'] : '';
        $insert_time_end = isset($input['insert_time_end']) ? $input['insert_time_end'] : '';
        if (!empty($insert_time_start) && !empty($insert_time_end)) {
            $where['insert_time'] = ['between', [$insert_time_start, $insert_time_end]];
        }


        $partner_code = isset($input['partner_code']) ? $input['partner_code'] : '';
        if(!empty($partner_code)) {
            $pUser = Db::name('user_manage')->where('code',$partner_code)->find();
            if($pUser){
                $p =  Db::name('partner')->where('uid',$pUser['id'])->find();
                if($p){
                    $where['u.partner_id'] = $p['id'];
                }
            }
        }






        //排序字段
        $field = isset($input['field']) ? $input['field'] : 'o.id';
        $sort = isset($input['sort']) ? $input['sort'] : 'desc';
        //分页
        $limit = 20;
        $page = isset($input['page']) ? $input['page'] : 1;
        $offset = ($page - 1) * $limit;

        $data = db('tb_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->join('user_manage p', 'u.par_id=p.id', 'LEFT')
            ->where($where)
            ->order($field . ' ' . $sort)
            ->limit($offset, $limit)
            ->field('order_par_id,order_time,goods_price,user_buy_price,order_type,order_status,order_ok_time,rebate_fee,u.nickname,u.is_vip,super_partner_rebate_fee,p.nickname as par_name,is_rebate,user_rebate_fee,par_user_rebate_fee,super_partner_rebate_fee,partner_rebate_fee,u.partner_id')
            ->select();

        //总数据条数
        $total_num = db('tb_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->join('user_manage p', 'u.par_id=p.id', 'LEFT')
            ->where($where)
            ->count();
        //总页数
        $page_num = ceil($total_num / $limit);
        if (!empty($data)) {
            foreach ($data as $key => $value) {
                $data[$key]['nickname'] = emoji_decode( $data[$key]['nickname']);
                $data[$key]['par_name'] = emoji_decode( $data[$key]['par_name']);

                switch ($value['order_type']) {
                    case 1:
                        $order_type = '正常订单';
                        break;
                    case 2:
                        $order_type = '零元购';
                        break;
                    case 3:
                        $order_type = '积分商城';
                        break;
                }
                $data[$key]['order_type'] = $order_type;
                switch ($value['order_status']) {
                    case 0:
                        $order_status = '无效';
                        break;
                    case 1:
                        $order_status = '已付款';
                        break;
                    case 2:
                        $order_status = '已收货';
                        break;
                    case 3:
                        $order_status = '已结算';
                        break;
                    default:
                        $order_status = '无效';
                        break;
                }
                $data[$key]['order_status'] = $order_status;
                switch ($value['is_vip']) {
                    case 0:
                        $is_vip = '否';
                        break;
                    case 1:
                        $is_vip = '是';
                        break;
                }
                $data[$key]['is_vip'] = !empty($is_vip)  ? $is_vip : '否';
                switch ($value['is_rebate']) {
                    case 0:
                        $is_rebate = '待返利';
                        break;
                    case 1:
                        $is_rebate = '已返利';
                        break;
                }
                $data[$key]['is_rebate'] = $is_rebate;

                $partner_uid = Db::name('partner')->where('id',$data[$key]['partner_id'])->value('uid');
                $partner_user = Db::name('partner')->where('id',$partner_uid)->find();
                $data[$key]['partner_nickname'] =!empty( $partner_user['nickname']) ? emoji_decode($partner_user['nickname']) : '';
                $data[$key]['partner_code'] = !empty( $partner_user['code']) ?$partner_user['code'] : '';









            }
            returnSuccess(['data' => $data, 'total_num' => $total_num, 'page_num' => $page_num], '订单查询成功');
        } else {
            returnSuccess([],'暂无数据');
        }
    }

    /**
     * 京东订单
     */
    public function jdOrders()
    {
        $id = $this->id;
//        $id = 1;
        $input = input();
        $where = [];
        $where['u.super_partner_id'] = $id;
        $order_no = isset($input['order_no']) ? $input['order_no'] : '';
        if (!empty($order_no)) {
            $where['order_id'] = $order_no;
        }
        $order_time_start = isset($input['order_time_start']) ? strtotime($input['order_time_start']) * 1000 : '';
        $order_time_end = isset($input['order_time_end']) ? strtotime($input['order_time_end']) * 1000 : '';
        if (!empty($order_time_start) && !empty($order_time_end)) {
            $where['order_time'] = ['between', [$order_time_start, $order_time_end]];
        }
        $order_status = isset($input['order_status']) ? $input['order_status'] : '';
        if (!empty($order_status)) {
            $where['order_status'] = $order_status;
        }
        $order_ok_time_start = isset($input['order_ok_time_start']) ? strtotime($input['order_ok_time_start']) * 1000 : '';
        $order_ok_time_end = isset($input['order_ok_time_end']) ? strtotime($input['order_ok_time_end']) * 1000 : '';
        if (!empty($order_ok_time_start) && !empty($order_ok_time_end)) {
            $where['order_ok_time'] = ['between', [$order_ok_time_start, $order_ok_time_end]];
        }
        $nickname = isset($input['nickname']) ? $input['nickname'] : '';
        if (!empty($nickname)) {
            $where['u.nickname'] = $nickname;
        }
        $par_name = isset($input['par_name']) ? $input['par_name'] : '';
        if (!empty($par_name)) {
            $where['p.nickname'] = $par_name;
        }
        $is_rebate = isset($input['is_rebate']) ? $input['is_rebate'] : '';
        if (!empty($is_rebate)) {
            $where['is_rebate'] = $is_rebate;
        }
        //返利时间
        $insert_time_start = isset($input['insert_time_start']) ? $input['insert_time_start'] : '';
        $insert_time_end = isset($input['insert_time_end']) ? $input['insert_time_end'] : '';
        if (!empty($insert_time_start) && !empty($insert_time_end)) {
            $where['insert_time'] = ['between', [$insert_time_start, $insert_time_end]];
        }


        $partner_code = isset($input['partner_code']) ? $input['partner_code'] : '';
        if(!empty($partner_code)) {
            $pUser = Db::name('user_manage')->where('code',$partner_code)->find();
            if($pUser){
                $p =  Db::name('partner')->where('uid',$pUser['id'])->find();
                if($p){
                    $where['u.partner_id'] = $p['id'];
                }
            }
        }



        //排序字段
        $field = isset($input['field']) ? $input['field'] : 'o.id';
        $sort = isset($input['sort']) ? $input['sort'] : 'desc';
        //分页
        $limit = 20;
        $page = isset($input['page']) ? $input['page'] : 1;
        $offset = ($page - 1) * $limit;

        $data = db('jd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->join('user_manage p', 'u.par_id=p.id', 'LEFT')
            ->where($where)
            ->order($field . ' ' . $sort)
            ->limit($offset, $limit)
            ->field('order_id,order_time,goods_price,user_buy_price,order_status,order_ok_time,rebate_fee,u.nickname,u.is_vip,super_partner_rebate_fee,p.nickname as par_name,is_rebate,user_rebate_fee,par_user_rebate_fee,super_partner_rebate_fee,partner_rebate_fee,u.partner_id')
            ->select();
        //总数据条数
        $total_num = db('jd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->join('user_manage p', 'u.par_id=p.id', 'LEFT')
            ->where($where)
            ->count();
        //总页数
        $page_num = ceil($total_num / $limit);
        if (!empty($data)) {
            foreach ($data as $key => $value) {
                $data[$key]['nickname'] = emoji_decode( $data[$key]['nickname']);
                $data[$key]['par_name'] = emoji_decode( $data[$key]['par_name']);
                switch ($value['order_status']) {
                    case 0:
                        $order_status = '无效';
                        break;
                    case 1:
                        $order_status = '已付款';
                        break;
                    case 2:
                        $order_status = '已收货';
                        break;
                    case 3:
                        $order_status = '已结算';
                        break;
                    default:
                        $order_status = '无效';
                        break;
                }
                $data[$key]['order_status'] = $order_status;
                switch ($value['is_vip']) {
                    case 0:
                        $is_vip = '否';
                        break;
                    case 1:
                        $is_vip = '是';
                        break;
                }
                $data[$key]['is_vip'] = !empty($is_vip)  ? $is_vip : '否';
                switch ($value['is_rebate']) {
                    case 0:
                        $is_rebate = '待返利';
                        break;
                    case 1:
                        $is_rebate = '已返利';
                        break;
                }
                $data[$key]['is_rebate'] = $is_rebate;
                $data[$key]['order_time'] = date('Y-m-d H:i:s', $value['order_time'] / 1000);
                $data[$key]['order_ok_time'] = $value['order_ok_time'] == 0 ? '-' : date('Y-m-d H:i:s', $value['order_ok_time'] / 1000);

                $partner_uid = Db::name('partner')->where('id',$data[$key]['partner_id'])->value('uid');
                $partner_user = Db::name('partner')->where('id',$partner_uid)->find();
                $data[$key]['partner_nickname'] =!empty( $partner_user['nickname']) ? emoji_decode($partner_user['nickname']) : '';
                $data[$key]['partner_code'] = !empty( $partner_user['code']) ?$partner_user['code'] : '';

            }
            returnSuccess(['data' => $data, 'total_num' => $total_num, 'page_num' => $page_num], '订单查询成功');
        } else {
            returnSuccess([],'暂无数据');
        }
    }

    /**
     * 拼多多订单
     */
    public function pddOrders()
    {
        $id = $this->id;
//        $id = 1;
        $input = input();
        $where = [];
        $where['u.super_partner_id'] = $id;
        $order_no = isset($input['order_no']) ? $input['order_no'] : '';
        if (!empty($order_no)) {
            $where['order_id'] = $order_no;
        }
        $order_time_start = isset($input['order_time_start']) ? strtotime($input['order_time_start']) : '';
        $order_time_end = isset($input['order_time_end']) ? strtotime($input['order_time_end']) : '';
        if (!empty($order_time_start) && !empty($order_time_end)) {
            $where['order_time'] = ['between', [$order_time_start, $order_time_end]];
        }
        $order_status = isset($input['order_status']) ? $input['order_status'] : '';
        if (!empty($order_status)) {
            $where['order_status'] = $order_status;
        }
        $order_ok_time_start = isset($input['order_ok_time_start']) ? strtotime($input['order_ok_time_start']) : '';
        $order_ok_time_end = isset($input['order_ok_time_end']) ? strtotime($input['order_ok_time_end']) : '';
        if (!empty($order_ok_time_start) && !empty($order_ok_time_end)) {
            $where['order_ok_time'] = ['between', [$order_ok_time_start, $order_ok_time_end]];
        }
        $nickname = isset($input['nickname']) ? $input['nickname'] : '';
        if (!empty($nickname)) {
            $where['u.nickname'] = $nickname;
        }
        $par_name = isset($input['par_name']) ? $input['par_name'] : '';
        if (!empty($par_name)) {
            $where['p.nickname'] = $par_name;
        }
        $is_rebate = isset($input['is_rebate']) ? $input['is_rebate'] : '';
        if (!empty($is_rebate)) {
            $where['is_rebate'] = $is_rebate;
        }
        //返利时间
        $insert_time_start = isset($input['insert_time_start']) ? $input['insert_time_start'] : '';
        $insert_time_end = isset($input['insert_time_end']) ? $input['insert_time_end'] : '';
        if (!empty($insert_time_start) && !empty($insert_time_end)) {
            $where['insert_time'] = ['between', [$insert_time_start, $insert_time_end]];
        }




        $partner_code = isset($input['partner_code']) ? $input['partner_code'] : '';
        if(!empty($partner_code)) {
            $pUser = Db::name('user_manage')->where('code',$partner_code)->find();
            if($pUser){
                $p =  Db::name('partner')->where('uid',$pUser['id'])->find();
                if($p){
                    $where['u.partner_id'] = $p['id'];
                }
            }
        }









        //排序字段
        $field = isset($input['field']) ? $input['field'] : 'o.id';
        $sort = isset($input['sort']) ? $input['sort'] : 'desc';
        //分页
        $limit = 20;
        $page = isset($input['page']) ? $input['page'] : 1;
        $offset = ($page - 1) * $limit;

        $data = db('pdd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->join('user_manage p', 'u.par_id=p.id', 'LEFT')
            ->where($where)
            ->order($field . ' ' . $sort)
            ->limit($offset, $limit)
            ->field('order_id,order_time,goods_price,user_buy_price,order_status,order_ok_time,rebate_fee,u.nickname,u.is_vip,super_partner_rebate_fee,p.nickname as par_name,is_rebate,user_rebate_fee,par_user_rebate_fee,super_partner_rebate_fee,partner_rebate_fee,u.partner_id')
            ->select();
        //总数据条数
        $total_num = db('pdd_order')
            ->alias('o')
            ->join('user_manage u', 'o.uid=u.id', 'LEFT')
            ->join('user_manage p', 'u.par_id=p.id', 'LEFT')
            ->where($where)
            ->count();
        //总页数
        $page_num = ceil($total_num / $limit);
        if (!empty($data)) {
            foreach ($data as $key => $value) {
                $data[$key]['nickname'] = emoji_decode( $data[$key]['nickname']);
                $data[$key]['par_name'] = emoji_decode( $data[$key]['par_name']);

                switch ($value['order_status']) {
                    case 0:
                        $order_status = '无效';
                        break;
                    case 1:
                        $order_status = '已付款';
                        break;
                    case 2:
                        $order_status = '已收货';
                        break;
                    case 3:
                        $order_status = '已结算';
                        break;
                    default:
                        $order_status = '无效';
                        break;
                }
                $data[$key]['order_status'] = $order_status;
                switch ($value['is_vip']) {
                    case 0:
                        $is_vip = '否';
                        break;
                    case 1:
                        $is_vip = '是';
                        break;
                }
                $data[$key]['is_vip'] = !empty($is_vip)  ? $is_vip : '否';
                switch ($value['is_rebate']) {
                    case 0:
                        $is_rebate = '待返利';
                        break;
                    case 1:
                        $is_rebate = '已返利';
                        break;
                }
                $data[$key]['is_rebate'] = $is_rebate;
                $data[$key]['order_time'] = date('Y-m-d H:i:s', $value['order_time']);
                $data[$key]['order_ok_time'] = empty($value['order_ok_time']) ? '-' : date('Y-m-d H:i:s', $value['order_ok_time']);





                $partner_uid = Db::name('partner')->where('id',$data[$key]['partner_id'])->value('uid');
                $partner_user = Db::name('partner')->where('id',$partner_uid)->find();
                $data[$key]['partner_nickname'] =!empty( $partner_user['nickname']) ? emoji_decode($partner_user['nickname']) : '';
                $data[$key]['partner_code'] = !empty( $partner_user['code']) ?$partner_user['code'] : '';




            }
            returnSuccess(['data' => $data, 'total_num' => $total_num, 'page_num' => $page_num], '订单查询成功');
        } else {
            returnSuccess([],'暂无数据');
        }
    }
}